<?php
namespace Plu\Library\Models\Platform\Album;
use Phalcon\Mvc\Model;

class AlbumOfMedia extends Model {
    public $Id;
    public $AlbumId;
    public $MediaId;    
    public $WriteTime;

    public function initialize() {
        $this->setReadConnectionService('db.LongzhuMediaReadOnly');
        //$this->belongsTo('AlbumId', 'Album', 'Id');
    }

    public function getSource() {
        return "AlbumOfMedia";
    }

    /**
     * 根据最后更新时间获取专辑和mediaid
     */
    public function getAlbumsByWritetime($albumids) {
        $sql = array();
        $len = count($albumids);
        $params = array();

        for ($c = 0; $c < $len; $c++) {
            $p = "a{$c}";
            $sql[] = " AlbumId = :{$p}: ";
            $params[$p] = $albumids[$c];
        }
        $sql = '(' . join(' OR ', $sql) . ')';

        $phql = "SELECT AlbumId, MediaId, MAX(WriteTime) as WriteTime FROM Plu\Library\Models\Platform\Album\AlbumOfMedia WHERE" . $sql  . "GROUP BY AlbumId ORDER BY WriteTime DESC";
        $modelsManager = $this->getDI()->get('modelsManager');
        $items = $modelsManager->executeQuery($phql, $params);

        if (count($items)) {
            return $items->toArray();
        }

        return false;
    }
}

?>
